package com.ruoyi.project.module.response.controller;

import com.alibaba.fastjson.JSONException;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.module.project.domain.Project;
import com.ruoyi.project.module.project.service.IProjectService;
import com.ruoyi.project.module.response.domain.Response;
import com.ruoyi.project.module.response.domain.ResponseParam;
import com.ruoyi.project.module.response.service.IResponseParamService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName ResponseParamController
 * @Description TODO: 接口返回值
 * @Author Rick
 * @Date 2018/10/15 10:22
 * @Vesion 1.0.0
 */
@Controller
@RequestMapping("/module/response")
public class ResponseParamController extends BaseController {

    private String prefix = "module/response";


    @Resource
    private IResponseParamService responseParamService;

    @Resource
    private IProjectService projectService;


    /**
     * 查询接口项目列表
     */
    @ResponseBody
    @GetMapping("/listtree")
//    @RequiresPermissions("module:project:list")
    public List<ResponseParam> listtree(@RequestParam(value = "apiProjectId",required = false) Long apiProjectId) {
        return (responseParamService.getList(apiProjectId));
    }

    @ResponseBody
    @PostMapping("/add")
//    @RequiresPermissions("module:project:list")
    public AjaxResult add(@RequestParam("paramJson") String paramJson, @RequestParam("projectId") Long projectId
            ,@RequestParam(value = "ignorePageInfo",defaultValue = "false") boolean ignorePageInfo) {
        try{
            return toAjax(responseParamService.addResponseParam(paramJson,projectId,ignorePageInfo));
        }catch (JSONException e1){
            e1.printStackTrace();
            return error(1,"json格式错误");
        }catch(Exception e){
            e.printStackTrace();
            return error(1,"系统错误!");
        }
    }

    @ResponseBody
    @PostMapping("/addline")
//    @RequiresPermissions("module:project:list")
    public AjaxResult addline(Response response) {
        try{
            return toAjax(responseParamService.addNewLine(response));
        }catch(Exception e){
            e.printStackTrace();
            return error(1,"系统错误!");
        }
    }

    @ResponseBody
    @PostMapping("/update")
//    @RequiresPermissions("module:project:list")
    public AjaxResult update(@RequestParam("value") Object value,@RequestParam("modifyField") String modifyField, @RequestParam("responseId") Long responseId) {
        try{
            return toAjax(responseParamService.modifyResponse(value,modifyField,responseId));
        }catch(Exception e){
            e.printStackTrace();
            return error(1,"系统错误!");
        }
    }

    @ResponseBody
    @PostMapping("/clean")
//    @RequiresPermissions("module:project:list")
    public AjaxResult cleanResponse(@RequestParam("projectId") Long projectId) {
        try{
            return toAjax(responseParamService.cleanResponse(projectId));
        }catch(Exception e){
            e.printStackTrace();
            return error(1,"系统错误!");
        }
    }

    @ResponseBody
    @PostMapping("/remove/{responseCode}")
//    @RequiresPermissions("module:project:list")
    public AjaxResult remove(@PathVariable("responseCode") String responseCode) {
        try {
            return toAjax(responseParamService.delResponse(responseCode));
        }catch(Exception e){
            e.printStackTrace();
            return error(1,"系统错误!");
        }
    }

    /**
     * 新增返回参数
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Integer parentId, ModelMap mmap ) {
        Project info = projectService.selectProjectById(parentId);
        mmap.put("project", info);
        return prefix + "/add";
    }



}
